package com.googlecode.easyibatis.core.model;

import java.io.Serializable;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.math.NumberUtils;

/**
 * 
 * @author zxq
 * 
 */
@SuppressWarnings("serial")
public abstract class Model implements Serializable {
	/**
	 * 编号
	 */
	private Long id;

	/**
    * 
    */
	public Model() {
		super();
	}

	/**
	 * @param id
	 */
	public Model(Long id) {
		super();
		this.id = id;
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		if (id == null) {
			return NumberUtils.LONG_ZERO;
		}
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this.id == null ? this : this.id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj != null && obj.getClass().isInstance(this)) {
			return EqualsBuilder.reflectionEquals(((Model) obj).getId(), this.id);
		}
		return false;
	}

	/**
	 * overriding methods (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}
